package services.impl;

import dao.UserMapper;
import domain.User;
import org.springframework.stereotype.Service;
import services.UserService;
import utils.PageBean;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public List<User> queryUserByPage(String userName, int userRole, PageBean pageBean) {
        return userMapper.queryUserByPage(userName,userRole,pageBean);
    }

    @Override
    public User login(String userCode, String userPassword) {
        return userMapper.login(userCode,userPassword);
    }

    @Override
    public void totalCount(PageBean pageBean) {
        int totalCount = userMapper.totalCount();
        pageBean.setTotal(totalCount);
    }

    @Override
    public Boolean modifyPassword(int id, String newPassword) {
        int row = userMapper.modifyPassword(id,newPassword);
        boolean flag = false;
        if(row>0){
            flag = true;
        }
        return flag;
    }

    @Override
    public boolean addUser(User user) {
        boolean flag = false;
        int row = userMapper.addUser(user);
        if(row>0){
            flag = true;
        }
        return flag;
    }

    @Override
    public boolean checkUserCode(String userCode) {
        boolean flag = false; //不可用
        int row = userMapper.checkUserCode(userCode);
        if(row==0){ //不存在
            flag = true; //可用
        }
        return flag;
    }

    @Override
    public boolean deleteUser(int id) {
        boolean flag = false;
        int row = userMapper.deleteUser(id);
        if(row>0){ //删除成功
            flag = true;
        }
        return flag;
    }

    @Override
    public User queryUserById(int id) {
        return userMapper.queryUserById(id);
    }

    @Override
    public boolean updateUser(User user) {
        boolean flag  = false;
        int row = userMapper.updateUser(user);
        if(row>0){
            flag = true; //修改成功
        }
        return flag;
    }
}
